//
//  LoginViewController.m
//  project
//
//  Created by 黄建华 on 2018/1/8.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "LoginViewController.h"
#import "RegistrationViewController.h"
#import "SNPersonSetViewController.h"
#import "AppDelegate.h"
#import "KeyChainStore.h"
#import "FeedBackViewController.h"
#import "ForgetEmailViewController.h"

@interface LoginViewController ()<UITextViewDelegate>{
    BOOL     inChina;
    NSString *openId;
    NSString *openType;
}

@property (nonatomic, strong) UILabel     *naTitle;
@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic, strong) UITextField *textFiled1;
@property (nonatomic, strong) UITextField *textFiled2;
@property (nonatomic, strong) UIButton    *loginButton;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    [self loginOnPlayMP4];
    
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:self.view.frame];
    bgImageView.userInteractionEnabled = YES;
    bgImageView.alpha                  = 0.9;
    bgImageView.image                  = [UIImage imageNamed:@"动画叠层"];
    [self.view addSubview:bgImageView];
    
    openType = [NSString string];
    openId   = [NSString string];

    UIView *loginView = [[UIView alloc] init];
    loginView.frame               = CGRectMake(0, 0, screenWidth -40, 260);
    loginView.backgroundColor     = WELLGOCOLORWHITE;
    loginView.alpha               = 0.20;
    loginView.layer.cornerRadius  = 5.0;
    loginView.layer.masksToBounds = YES;
    loginView.center              = CGPointMake(self.view.centerX, self.view.centerY - 60);
    [self.view addSubview:loginView];
    
    _textFiled1           = [[UITextField alloc] init];
    _textFiled1.tintColor = [UIColor whiteColor];
    _textFiled1.frame     = CGRectMake(55, loginView.y + 20, kSCREEN_WIDTH-110, 25);
    [_textFiled1 changePlaceholder:HHH_Text(@"请输入邮箱或手机号") withColor:HHHFONTCOLOR withFont:15.0];
    _textFiled1.textColor = WELLGOCOLORWHITE;
    _textFiled1.delegate  = self;
    if ([SN_USER.loginAccount contains:@"@"]) {
        _textFiled1.text = SN_USER.loginAccount;
    }
    [self.view addSubview:_textFiled1];
    [self.view bringSubviewToFront:_textFiled1];
    
    UIView *line1 = [[UIView alloc] init];
    line1.backgroundColor = SNCOLORWHITE;
    line1.alpha           = 0.33;
    line1.frame           = CGRectMake(_textFiled1.x -5, _textFiled1.bottom + 5, _textFiled1.width +10, 1);
    [self.view addSubview:line1];
    
    _textFiled2           = [[UITextField alloc] init];
    _textFiled2.tintColor = [UIColor whiteColor];
    _textFiled2.frame     = CGRectMake(55, _textFiled1.bottom+25, kSCREEN_WIDTH-110, 25);
    [_textFiled2 changePlaceholder:HHH_Text(@"请输入密码") withColor:HHHFONTCOLOR withFont:15.0];
    _textFiled2.textColor       = SNCOLORWHITE;
    _textFiled2.delegate        = self;
    _textFiled2.secureTextEntry = YES;
    [self.view addSubview:_textFiled2];
    [self.view bringSubviewToFront:_textFiled2];
    
    UIView *line2 = [[UIView alloc] init];
    line2.alpha           = 0.33;
    line2.backgroundColor = SNCOLORWHITE;
    line2.frame           = CGRectMake(_textFiled2.x -5, _textFiled2.bottom + 5, _textFiled2.width +10, 1);
    [self.view addSubview:line2];
    
    UIButton *forgetButton = [[UIButton alloc] initWithFrame:CGRectMake(_textFiled2.right - 140, _textFiled2.bottom + 5, 140, 40)];
    forgetButton.titleLabel.font = SNSFONT(13.0);
    [forgetButton setTitle:SN_Text(@"忘记密码?") forState:UIControlStateNormal];
    forgetButton.titleLabel.textAlignment = NSTextAlignmentRight;
    [forgetButton setTitleColor:SNCOLORGRAY forState:UIControlStateHighlighted];
    [forgetButton setTitleColor:WELLGOCOLORWHITE forState:UIControlStateNormal];
    [forgetButton addTarget:self action:@selector(forgetButtonClick) forControlEvents:UIControlEventTouchUpInside];
    CGSize titleSize = [forgetButton.titleLabel.text sizeWithFont:forgetButton.titleLabel.font maxSize:CGSizeMake(MAXFLOAT, 40)];
    forgetButton.frame = CGRectMake(_textFiled2.right - titleSize.width, _textFiled2.origin.y + _textFiled2.height + 5, titleSize.width, 40);
    [self.view addSubview:forgetButton];
    [self.view bringSubviewToFront:forgetButton];
    
    UIView *view3 = [[UIView alloc] init];
    view3.frame               = CGRectMake(50, forgetButton.bottom, kSCREEN_WIDTH - 100, 40);
    view3.backgroundColor     = WELLGOCOLORWHITE;
    view3.alpha               = 0.10;
    view3.layer.cornerRadius  = 5.0;
    view3.layer.masksToBounds = YES;
    [self.view addSubview:view3];
    
    UIView *view4 = [[UIView alloc] init];
    view4.frame               = CGRectMake(50, view3.bottom + 5, kSCREEN_WIDTH - 100, 40);
    view4.backgroundColor     = [UIColor clearColor];
    view4.layer.cornerRadius  = 5.0;
    view4.layer.borderColor   = SNCOLORGRAYLINE.CGColor;
    view4.layer.borderWidth   = 1.0;
    view4.layer.masksToBounds = YES;
    [self.view addSubview:view4];
    
    
    _loginButton                     = [[UIButton alloc] init];
    _loginButton.frame               = CGRectMake(50, forgetButton.bottom, kSCREEN_WIDTH - 100, 40);
    _loginButton.layer.cornerRadius  = 5;
    _loginButton.layer.borderColor   = SNCOLORGRAYLINE.CGColor;
    _loginButton.layer.borderWidth   = 1.0;
    _loginButton.layer.masksToBounds = YES;
    [_loginButton setTitleNormal:HHH_Text(@"登录")];
    [_loginButton.titleLabel setFont:SNSFONT(12)];
    [self.view addSubview:_loginButton];
    [_loginButton setTitleColor:SNCOLORGRAY forState:UIControlStateHighlighted];
    [_loginButton addTarget:self action:@selector(loginButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [_loginButton setTitleColor:SNCOLORWHITE forState:UIControlStateNormal];
    [self.view bringSubviewToFront:_loginButton];
    
    NSArray *imageArr = @[@"qq(1)", @"微信(1)", @"推特"];
    for (int i = 0; i < 3; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame  = CGRectMake(0, 0, view4.width/3, 40);
        button.tag    = 100+i;
        button.center = CGPointMake(view4.x+view4.width/6+view4.width/3*i, view4.center.y);
        [button setImageNormal:imageArr[i]];
        [button addTouchUpTarget:self action:@selector(thirdLogin:)];
        [self.view addSubview:button];
        [self.view bringSubviewToFront:button];
        
        if (i > 0) {
            UIView *sLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, 30)];
            sLine.backgroundColor = WELLGOCOLORWHITE;
            sLine.center          = CGPointMake(view4.x+view4.width/3*i-0.5, view4.center.y);
            [self.view addSubview:sLine];
            [self.view bringSubviewToFront:sLine];
        }
    }
    
    UIButton *feedbackBtn = [[UIButton alloc]initWithFrame:CGRectMake(loginView.left, view4.bottom+5, loginView.width, 30)];
    [feedbackBtn setTitleNormal:SN_Text(@"登錄與註冊遇到問題")];
    [feedbackBtn addTouchUpTarget:self action:@selector(showFeedbackVC)];
    feedbackBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:feedbackBtn];
    [self.view bringSubviewToFront:feedbackBtn];

    CGFloat width = screenWidth/2 - 20;
    
    UIView *view1 = [[UIView alloc] init];
    view1.layer.cornerRadius  = 6.0;
    view1.layer.masksToBounds = YES;
    view1.frame               = CGRectMake(10 + 2, loginView.bottom+FitH(25), width, 55);
    view1.backgroundColor     = [UIColor whiteColor];
    view1.alpha               = 0.25;
    [bgImageView addSubview:view1];
    
    UIView *view2 = [[UIView alloc] init];
    view2.layer.cornerRadius  = 6.0;
    view2.layer.masksToBounds = YES;
    view2.frame               = CGRectMake(screenWidth/2 +8, loginView.bottom+FitH(25), width, 55);
    view2.backgroundColor     = [UIColor whiteColor];
    view2.alpha               = 0.25;
    [bgImageView addSubview:view2];
    
    UIButton *touristButton = [[UIButton alloc] initWithFrame:CGRectMake(10 + 2, loginView.bottom+FitH(25), width, 55)];
    touristButton.layer.borderWidth   = 1.0;
    touristButton.layer.borderColor   = WELLGOCOLORWHITE.CGColor;
    touristButton.layer.cornerRadius  = 6.0;
    touristButton.layer.masksToBounds = YES;
    [touristButton setTitle:SN_Text(@"游客登录") forState:UIControlStateNormal];
    [touristButton setTitleColor:SNCOLORGRAY forState:UIControlStateHighlighted];
    [touristButton setTitleColor:WELLGOCOLORWHITE forState:UIControlStateNormal];
    [touristButton addTarget:self action:@selector(touristLoginClick) forControlEvents:UIControlEventTouchUpInside];
    [bgImageView addSubview:touristButton];
    [bgImageView bringSubviewToFront:touristButton];
    
    UIButton *registerButton = [[UIButton alloc] initWithFrame:CGRectMake(screenWidth/2 +8, loginView.bottom+FitH(25), width, 55)];
    registerButton.layer.borderWidth   = 1.0;
    registerButton.layer.borderColor   = WELLGOCOLORWHITE.CGColor;
    registerButton.layer.cornerRadius  = 6.0;
    registerButton.layer.masksToBounds = YES;
    
    [registerButton setTitle:SN_Text(@"注册") forState:UIControlStateNormal];
    [registerButton setTitleColor:SNCOLORGRAY forState:UIControlStateHighlighted];
    [registerButton setTitleColor:WELLGOCOLORWHITE forState:UIControlStateNormal];
    [registerButton addTarget:self action:@selector(registerButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [bgImageView addSubview:registerButton];
    [bgImageView bringSubviewToFront:registerButton];
    
    UILabel *backLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, registerButton.bottom+FitH(30), HHHWIDTH, 30)];
    backLabel.text = SN_Text(@"登錄並接受服务条款");
    CGSize titleSize2 = [backLabel.text sizeWithFont:backLabel.font maxSize:CGSizeMake(MAXFLOAT, 30)];
    backLabel.frame = CGRectMake((HHHWIDTH-titleSize2.width)/2 +5, registerButton.bottom+10, titleSize2.width, 30);
    backLabel.text = @"";
    [bgImageView addSubview:backLabel];
    backLabel.userInteractionEnabled = YES;
    [bgImageView bringSubviewToFront:backLabel];
    
    UILabel *preLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, HHHWIDTH, 30)];
    preLabel.text = SN_Text(@"登錄並接受");
    preLabel.textAlignment = NSTextAlignmentLeft;
    preLabel.textColor = WELLGOCOLORWHITE;
    CGSize titleSize3 = [preLabel.text sizeWithFont:preLabel.font maxSize:CGSizeMake(MAXFLOAT, 30)];
    preLabel.frame = CGRectMake(0, 0, titleSize3.width, 30);
    [backLabel addSubview:preLabel];
    
    UILabel *serviceLabel = [[UILabel alloc]initWithFrame:CGRectMake(preLabel.right, 0, backLabel.width-preLabel.right, 30)];
    serviceLabel.text = SN_Text(@"服务条款");
    serviceLabel.textAlignment = NSTextAlignmentLeft;
    serviceLabel.textColor = [UIColor greenColor];
    [backLabel addSubview:serviceLabel];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showPolicy)];
    [serviceLabel addGestureRecognizer:tap];
    serviceLabel.userInteractionEnabled = YES;
    
    UIImageView *infoImageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(view1.origin.x +10, self.view.bottom-60, 100, 50)];
    infoImageView1.image = [UIImage imageNamed:@"info1"];
    [self.view addSubview:infoImageView1];
    [self.view bringSubviewToFront:infoImageView1];
    
    UIImageView *infoImageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(view2.origin.x +10, self.view.bottom-60, 100, 50)];
    infoImageView2.image = [UIImage imageNamed:@"info2"];
    [self.view addSubview:infoImageView2];
    [self.view bringSubviewToFront:infoImageView2];
    
    NSString *language = [NSString getCurrentLanguage];
    if ([language isEqualToString:@"zh-Hans-CN"] || [language isEqualToString:@"zh-Hans"]) {
        inChina = YES;
    }
}

-(void)showFeedbackVC{
    FeedBackViewController *feedVC = [[FeedBackViewController alloc]init];
    [self.navigationController pushViewController:feedVC animated:YES];
}

- (void)showPolicy {
    HDAlertView *alertView = [HDAlertView alertViewWithTitle:HHH_Text(@"關於隱私與準確度") andMessage:HHH_Text(@"\n    •    感謝您的信任並且我們知道您的信息非常重要，所以應用程序部分功能需要您的允許才能正常工作.\n\n    •    本產品並非醫療裝置，且並不旨在用於診斷、治療、治愈或預防任何疾病.產品精確性並非試圖與醫療或科學測量裝置相比.")];
    
    [alertView addButtonWithTitle:SN_Text(@"确定") type:HDAlertViewButtonTypeDefault handler:^(HDAlertView *alertView) {
    }];
    [alertView show];
}

- (void)thirdLogin:(UIButton *)sender {
    if (sender.tag == 100) {
        [[ThirdLogin sharedInstance] QQLoginWithSuccess:^(id object) {
            [LOGINACCOUNT setObjectValue:@"QQ登录"];
            [self loginFinish:object];
        } fail:^(id object) {
        }];
    } else if (sender.tag == 101) {
        [[ThirdLogin sharedInstance] WechatLoginWithSuccess:^(id object) {
            [LOGINACCOUNT setObjectValue:@"微信登录"];
            [self loginFinish:object];
        } fail:^(id object) {
        }];
    } else {
        [[ThirdLogin sharedInstance] TwitterLoginWithSuccess:^(id object) {
            [LOGINACCOUNT setObjectValue:@"推特登录"];
            [self loginFinish:object];
        } fail:^(id object) {
        }];
    }
}

- (void)loginOnPlayMP4 {
    NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"wellgo" ofType:@"mp4"];
    
    NSLog(@"videoPath>>>>>%@", videoPath);
    NSURL *url = [NSURL fileURLWithPath:videoPath];
    // 1.创建AVPlayerItem
    AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
    // 2.创建AVPlayer
    SN_Player = [AVPlayer playerWithPlayerItem:item];
    // 3.添加AVPlayerLayer
    AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:SN_Player];
    layer.frame        = CGRectMake(0, 0, screenWidth, screenHeight);
    layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.view.layer addSublayer:layer];
    [SN_Player play];
    
    //给AVPlayerItem添加播放完成通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:SN_Player.currentItem];
}

- (void)playbackFinished:(NSNotification *)notification {
    [SN_Player seekToTime:CMTimeMake(0, 1)];
    [SN_Player play];
}

// 登录
- (void)loginButtonClick {
    [LOGINACCOUNT setObjectValue:_textFiled1.text];
    [LOGINPASSWORD setObjectValue:_textFiled2.text];
    BOOL error1 = NO;
    BOOL error2 = NO;
    if ([_textFiled1.text isEqualToString:@""]) {
        SHOWMBProgressHUD(nil,SN_Text(@"请输入邮箱或手机号"), nil, NO, 2.0f);
        return;
    }
    if (![self validateEmail:_textFiled1.text]) {
        error1 = YES;
    }
    
    if (![self valiMobile:_textFiled1.text]) {
        error2 = YES;
    }
    if (error1 && error2) {
        SHOWMBProgressHUD(nil,SN_Text(@"错误的账号"), nil, NO, 2.0f);
        return;
    }
    
    if (![_textFiled2.text isPassword]) {
        SHOWMBProgressHUD(nil,SN_Text( @"密码必须为6～14位的数字或字母"), nil, NO, 2.0);
        return;
    }
    SHOWMBProgressHUDIndeterminate(SN_Text(@"请等待..."), nil, @"登录中");
    [self performSelector:@selector(loginOnTimeOut) withObject:nil afterDelay:30.0];
    if (_textFiled1.text && _textFiled2.text) {
        [SN_Net loginOnWithsuccess:^(id object)
         {
             [self loginFinish:object];
         }                    fail:^(id object) {
             SHOWMBProgressHUD(nil, SN_Text(@"登录失败"), nil, NO, 1.0);
         }];
    } else {
        SHOWMBProgressHUD(nil, SN_Text(@"信息请填写完整"), nil, NO, 1.0);
    }
}

// 游客登录
- (void)touristLoginClick {
    openId   = KK_DeviceUUID;
    openType = @"guest";
    SHOWMBProgressHUDIndeterminate(SN_Text(@"请等待..."), nil, @"登录中");
    [self performSelector:@selector(loginOnTimeOut) withObject:nil afterDelay:30.0];
    [SN_Net loginOnWithOpenId:openId withOpenType:openType Success:^(id object) {
        [LOGINACCOUNT setObjectValue:@"游客登录"];
        [self loginFinish:object];
    } fail:^(id object) {
        SHOWMBProgressHUD(nil, SN_Text(@"登录失败"), nil, NO, 1.0);
    }];
}

- (void)loginOnTimeOut
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(loginOnTimeOut) object:nil];
     HIDDENMBProgressHUD;
}

- (void)loginFinish:(id)object {
    [self loginOnTimeOut];
    NSLog(@"登录成功>>>>>");
    SHOWMBProgressHUD(SN_Text(@"登录成功"), nil, nil, NO, 2.0);
    UserInfoModel *model    = object;
    NSString      *account  = [LOGINACCOUNT getObjectValue];
    NSString      *passWord = [LOGINPASSWORD getObjectValue];
    [KK_LastUser setObjectValue:model.user_id];
    [SN_PublicClass updateUser];
    SN_USER.loginAccount  = account;
    SN_USER.loginPassword = passWord;
    SN_USER.portrait      = model.portrait;
    SN_USER.is_first      = model.is_first;
    SN_USER.user_id       = model.user_id;
    SN_USER.access_token  = model.access_token;
    SN_USER.weight = model.weight;
    SN_USER.height = model.height;
    SN_USER.gender = model.gender;
    [SN_USER updateToDB];
    
    [SN_Net downImageHeardWith];
    DEF_WEAKSELF_(LoginViewController)
    if (SN_USER.is_first) {
        SNPersonSetViewController *vc = [[SNPersonSetViewController alloc] init];
        vc.type = 0;
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        [SN_Net getUserInfoWithSuccess:^(id object) {
            [weakSelf getUserInfoFinish:object];
        } fail:^(id object) {
        }];
    }
    if (model.app_id == 0) {
        [SN_Net updateUserAppid:^(id object) {
            
        } fail:^(id object) {
            
        }];
    }
}

- (void)getUserInfoFinish:(id)object {
    NSLog(@"登录并获取用户信息成功>>>>>");
    UserInfoModel *model = object;
    [KK_LastUser setObjectValue:model.user_id];
    
    // 登录成功后跳转个人信息设置界面
    HIDDENMBProgressHUD;
    [kK_App enterMainVc];
}

//忘记密码
- (void)forgetButtonClick {
    ForgetEmailViewController *vc = [[ForgetEmailViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

// 注册
- (void)registerButtonClick {
    RegistrationViewController *vc = [[RegistrationViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

+ (BOOL)validateEmail:(NSString *)email {
    NSString    *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest  = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

- (BOOL)validateEmail:(NSString *)email {
    NSString    *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest  = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [_textFiled1 resignFirstResponder];
    [_textFiled2 resignFirstResponder];
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField == _textFiled1) {
        BOOL error1 = NO;
        BOOL error2 = NO;
        if ([_textFiled1.text isEqualToString:@""]) {
            SHOWMBProgressHUD(nil, SN_Text(@"请输入邮箱或手机号"), nil, NO, 2.0f);
            return;
        }
        if (![self valiMobile:_textFiled1.text]) {
            error1 = YES;
        }
        if (![self isValidateEmail:_textFiled1.text]) {
            error2 = YES;
        }
        if (error1 && error2) {
            SHOWMBProgressHUD(nil, SN_Text(@"错误的账号"), nil, NO, 2.0f);
        }
    }
    if (textField == _textFiled2) {
        if (![_textFiled2.text isPassword]) {
           SHOWMBProgressHUD(nil,SN_Text( @"密码必须为6～14位的数字或字母"),nil, NO, 1.0);
        }
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *tem = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]componentsJoinedByString:@""];
    if (![string isEqualToString:tem]) {
        return NO;
    }
    return YES;
}

- (BOOL)isValidateEmail:(NSString *)email {
    NSString    *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest  = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

- (BOOL)valiMobile:(NSString *)mobile {
    if (mobile.length != 11) {
        return NO;
    }
    /**
     * 手机号码:
     * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[0, 1, 6, 7, 8], 18[0-9]
     * 移动号段: 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
     * 联通号段: 130,131,132,145,155,156,170,171,175,176,185,186
     * 电信号段: 133,149,153,170,173,177,180,181,189
     */
    NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|7[0135678]|8[0-9])\\d{8}$";
    /**
     * 中国移动：China Mobile
     * 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
     */
    NSString *CM = @"^1(3[4-9]|4[7]|5[0-27-9]|7[08]|8[2-478])\\d{8}$";
    /**
     * 中国联通：China Unicom
     * 130,131,132,145,155,156,170,171,175,176,185,186
     */
    NSString *CU = @"^1(3[0-2]|4[5]|5[56]|7[0156]|8[56])\\d{8}$";
    /**
     * 中国电信：China Telecom
     * 133,149,153,170,173,177,180,181,189
     */
    NSString *CT = @"^1(3[3]|4[9]|53|7[037]|8[019])\\d{8}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm     = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu     = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct     = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    if (([regextestmobile evaluateWithObject:mobile] == YES)
        || ([regextestcm evaluateWithObject:mobile] == YES)
        || ([regextestct evaluateWithObject:mobile] == YES)
        || ([regextestcu evaluateWithObject:mobile] == YES)) {
        return YES;
    } else {
        return NO;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

